from fastapi import APIRouter, Query,UploadFile
from tortoise.expressions import Q
from app.core.ctx import CTX_USER_ID
from app.models import User, Customers
from app.settings.config import settings
from app.controllers.customers.customers_follow  import customers_follow_controller
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.customers import CustomersFollowCreate, CustomersFollowUpdate

customers_follow_router = APIRouter()
model_name = "额度申请"


@customers_follow_router.get("/list", summary=f"查看{model_name}列表")
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(100, description="每页数量"),
        phone: str = Query('', description="法人电话"),
        name: str = Query('', description="法人姓名"),
        qymc: str = Query('', description="企业名称"),
):
    q = Q()
    if phone:
        q &= Q(phone__contains=phone)
    if name:
        q &= Q(name__contains=name)
    if qymc:
        q &= Q(qymc__contains=qymc)

    total, data = await customers_follow_controller.do_list(page=page, page_size=page_size, search=q)
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@customers_follow_router.get("/get", summary=f"查看{model_name}")
async def do_get(
        data_id: int = Query(..., description="ID"),
):
    data = await customers_follow_controller.do_detail(data_id=data_id)
    return Success(data=data)

@customers_follow_router.post("/create", summary=f"创建{model_name}")
async def do_create(
        category_in: CustomersFollowCreate,
):
    add_id = CTX_USER_ID.get()
    category_in.add_id = add_id
    await customers_follow_controller.do_create(obj_in=category_in)
    await Customers.filter(id=category_in.customer_id).update(status=1)
    return Success(msg="创建成功")

@customers_follow_router.post("/update", summary=f"更新{model_name}")
async def do_update(
        category_in: CustomersFollowUpdate,
):
    await customers_follow_controller.do_update(obj_in=category_in)
    return Success(msg="更新成功")

@customers_follow_router.delete("/delete", summary=f"删除{model_name}")
async def do_delete(
        data_id: int = Query(..., description="ID"),
):
    await customers_follow_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")




